package com.zhou.zhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.zhxy.mapper.AdminMapper;
import com.zhou.zhxy.pojo.Admin;
import com.zhou.zhxy.pojo.LoginForm;
import com.zhou.zhxy.service.AdminService;
import com.zhou.zhxy.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service  初始化 可以指定当前的实现类
 * Transactional  事务
 */
@Service("adminServiceImpl")
@Transactional
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    /**
     * 功能：1、管理员登录
     * @param loginForm
     * @return
     */
    @Override
    public Admin login(LoginForm loginForm) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",loginForm.getUsername());
        queryWrapper.eq("password", MD5.encrypt(loginForm.getPassword()));
        Admin admin = baseMapper.selectOne(queryWrapper);
        return admin;
    }

    /**
     * 功能：1、获取登录管理员的信息
     * @param userId
     * @return
     */
    @Override
    public Admin getAdminById(long userId) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        Admin admin = baseMapper.selectOne(queryWrapper);
        return admin;
    }

    /**
     * 功能：1、分页查询管理员的列表
     * @param page
     * @param admin
     * @return
     */
    @Override
    public IPage<Admin> getAdminList(Page<Admin> page, Admin admin) {
        QueryWrapper<Admin> queryWrapper = null;
        if(admin != null){
            queryWrapper = new QueryWrapper<>();
            if(admin.getName() != null){
                queryWrapper.eq("name",admin.getName());
            }
            queryWrapper.orderByDesc("id");
            queryWrapper.orderByAsc("name");
        }
        IPage<Admin> iPage = baseMapper.selectPage(page,queryWrapper);
        return iPage;
    }
}
